package org.cncy;

import lombok.extern.slf4j.Slf4j;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;

import java.io.ObjectStreamClass;
import java.io.Serializable;

/**
 * 位置信息上传
 */
@Slf4j
public class I3005JavaSampler extends AbstractJavaSamplerClient implements Serializable {
    private static final long serialVersionUID = ObjectStreamClass.lookup(I3005JavaSampler.class).getSerialVersionUID();

    private static final String URL = "url";
    private static final String TENANTID = "TenantID";

    private static final String ClientUID = "ClientUID";


    public I3005JavaSampler() {
        log.debug(Utils.whoAmI(this) + "\tConstruct");
    }


    //通过jmter的图形框进行传参,无默认值
    @Override
    public Arguments getDefaultParameters() {
        Arguments arguments = new Arguments();
        arguments.setComment("位置信息回传");
        arguments.addArgument(URL, "");
        arguments.addArgument(ClientUID, "");
        arguments.addArgument(TENANTID, "");

        return arguments;
    }

    public SampleResult runTest(JavaSamplerContext javaSamplerContext) {
        SampleResult results = new SampleResult();
        //返回请求值，体现在jmeter查看结果数请求里面
        String url = javaSamplerContext.getParameter(URL);
        String tenantID = javaSamplerContext.getParameter(TENANTID);
        String udid = javaSamplerContext.getParameter(ClientUID);

        log.debug("url = {},ClientUID = {},tenantID = {}", url, udid, tenantID);

        String body = "<?xml version='1.0' encoding='UTF-8' ?><Request><OperID><TenantID>cop</TenantID><ClientUID>" + udid + "</ClientUID><FlowNum></FlowNum><Timezone>-1</Timezone><Result>-1</Result><ClientVersion>2.0.148</ClientVersion><ClientBuildNum>200148</ClientBuildNum></OperID><DeviceUpdate><DeviceLocation><Longitude>110.4833670</Longitude><Latitude>40.0223240</Latitude><LocatedTime>" + System.currentTimeMillis() + "</LocatedTime><Location>中国</Location><FlowNum></FlowNum></DeviceLocation><UsageStats>0</UsageStats><OSVersion>14</OSVersion><SpecialTypeFlag>0</SpecialTypeFlag></DeviceUpdate></Request>";
        results.setSamplerData("请求体 : " + body);
        results.sampleStart();
        try {
            byte[] bytes = HttpsUtils.doPost(url, HttpsXMLUtil.getXMLRequestByte(3005, body));
            results.setSuccessful(true);//这里决定测试用例成功还是失败
            //设置返回信息
            results.setResponseCode("200");
            results.setResponseMessage("成功测试");
            //results.setSampleLabel("用户登录");
            //返回结果
            results.setResponseData(new String(Utils.interceptBody(bytes)), "utf-8");
        } catch (Exception e) {
            log.error("Exception = {}", e);
            results.setSuccessful(false);
        } finally {
            // Record end time and populate the results.
            results.sampleEnd();
        }
        if (log.isDebugEnabled()) {
            log.debug(Utils.whoAmI(this) + "\tTime:\t" + results.getTime());
        }

        return results;
    }

}
